﻿using IDLETD.Agent;
using IDLETD.FSM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace IDLETD.Models
{
    public class BaseGameObject : BaseModel
    {
        private string id = Guid.NewGuid().GetHashCode().ToString();
        public string Id
        {
            get { return id; }
            set { id = value; base.OnPropertyChanged("Id"); }
        }

        private ObjectType type = ObjectType.怪物;
        public ObjectType Type
        {
            get { return type; }
            set { type = value; base.OnPropertyChanged("Type"); }
        }

        private double x = 0.0;
        public double X
        {
            get { return x; }
            set { x = value; base.OnPropertyChanged("X"); }
        }

        private double y = 0.0;
        public double Y
        {
            get { return y; }
            set { y = value; base.OnPropertyChanged("Y"); }
        }

        private double width = 50;
        /// <summary>
        /// 宽
        /// </summary>
        public double Width
        {
            get { return width; }
            set { width = value; base.OnPropertyChanged("Width"); }
        }

        private double height = 50;
        /// <summary>
        /// 高
        /// </summary>
        public double Height
        {
            get { return height; }
            set { height = value; base.OnPropertyChanged("Height"); }
        }

        private double speed = 2.0;
        public double Speed
        {
            get { return speed; }
            set { speed = value; base.OnPropertyChanged("Speed"); }
        }

        private double opacity = 1.0;
        public double Opacity
        {
            get { return opacity; }
            set { opacity = value; base.OnPropertyChanged("Opacity"); }
        }

        private double hp = 1.0;
        public double Hp
        {
            get { return hp; }
            set { hp = value; base.OnPropertyChanged("Hp"); }
        }

        private double range = 200;
        /// <summary>
        /// 攻击距离
        /// </summary>
        public double Range
        {
            get { return range; }
            set { range = value; base.OnPropertyChanged("Range"); }
        }

        private double attackSpeed = 50;
        /// <summary>
        /// 攻击速度
        /// </summary>
        public double AttackSpeed
        {
            get { return attackSpeed; }
            set { attackSpeed = value; base.OnPropertyChanged("AttackSpeed"); }
        }

        private double deadSpeed = 0.1;
        /// <summary>
        /// 消失速率
        /// </summary>
        public double DeadSpeed
        {
            get { return deadSpeed; }
            set { deadSpeed = value; base.OnPropertyChanged("DeadSpeed"); }
        }

        private bool isDead = false;
        /// <summary>
        /// 是否死亡
        /// </summary>
        public bool IsDead
        {
            get { return isDead; }
            set { isDead = value;base.OnPropertyChanged("IsDead"); }
        }

        private bool isDestroy = false;
        /// <summary>
        /// 是否销毁
        /// </summary>
        public bool IsDestroy
        {
            get { return isDestroy; }
            set { isDestroy = value; base.OnPropertyChanged("IsDestroy"); }
        }

        #region 事件
        public event Action Remove;
        public event Action<BulletModel, MonsterModel> ShotBullet;
        #endregion

        #region 变量
        FSMCore fsm;
        #endregion

        public BaseGameObject()
        {
            fsm = new FSMCore() { Onwer = this };
        }

        public bool RegisterState(StateEnum key, StateBase value)
        {
            return fsm.RegisterState(key, value);
        }

        public void Excute()
        {
            fsm.Excute();
        }

        public bool GoIdle()
        {
            return fsm.ChangeState(StateEnum.空闲, null);
        }

        public bool GoScan()
        {
            return fsm.ChangeState(StateEnum.扫描, null);
        }

        public bool GoAttack(MonsterModel monster)
        {
            object[] objs = new object[1];
            objs[0] = monster;
            return fsm.ChangeState(StateEnum.攻击, objs);
        }

        public bool GoCool()
        {
            return fsm.ChangeState(StateEnum.冷却, null);
        }

        public bool GoMove(List<Point> path)
        {
            object[] objs = new object[1];
            objs[0] = path;
            return fsm.ChangeState(StateEnum.移动, objs);
        }

        public bool GoFollow(MonsterModel monster)
        {
            object[] objs = new object[1];
            objs[0] = monster;
            return fsm.ChangeState(StateEnum.追击, objs);
        }

        public bool GoDead()
        {
            return fsm.ChangeState(StateEnum.死亡, null);
        }

        public void CreateBullet(MonsterModel monster)
        {
            BulletModel bullet = new BulletModel() { speed = 10, width = 5, height = 5, X = X + Width / 2, Y = Y + Height / 2, DeadSpeed = 1 };
            if (ShotBullet != null) ShotBullet(bullet, monster);
        }

        public void BeHit(BulletModel bullet)
        {
            if (--Hp <= 0) 
                GoDead();
        }

        public void ObjectRemove()
        {
            if (Remove != null) Remove();
        }

    }
}
